package planesimulator.plane.wing.coefficients;

/**
 * Class that given set of 2D points 
 * interpolates value when given an argument.
 * @author Jakub Klimek
 */
public class ValueTable {

  /** Number of points */
  protected int n;
  
  /** X values of given points */
  protected float keys[];
  
  /** Y values of given points */
  protected float values[];

  /** 
   * Constructor 
   * @param n Number of points
   * @param keys X values of given points 
   * @param values Y values of given points
   */
  public ValueTable(int n, float keys[], float values[]) {
    this.n = n;
    this.keys = keys;
    this.values = values;
  }

  /**
   * Class that interpolates value given an argument
   * @param key X value of point
   * @return Interpolated Y value
   */
  public float interpolate(float key) {
    if (key < keys[0]) {
      return values[0];
    }
    if (key > keys[n - 1]) {
      return values[n - 1];
    }
    int i = 1;
    while (key > keys[i] && i < n - 1) {
      i++;
    }
    return values[i - 1] +
        (key - keys[i - 1]) / (keys[i] - keys[i - 1]) *
        (values[i] - values[i - 1]);
  }
}